﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Application.Permisos.ServiceAgent;
using System.Windows.Forms;

namespace Cesave.Presentation.Permisos.WinForms.Client.ViewModels.Permisos
{
	public partial class ExpedicionViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<CultivosEstacion> _cultivosEstaciones;

		/// <summary>
		/// 
		/// </summary>
		private CultivosEstacion _cultivosEstacion;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<CultivosEstacion> CultivosEstaciones
		{
			get
			{
				return _cultivosEstaciones;
			}
			private set
			{
				_cultivosEstaciones = value;
				if (_cultivosEstaciones != null)
					RaisePropertyChanged("CultivosEstaciones");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public CultivosEstacion CultivosEstacion
		{
			get
			{
				return _cultivosEstacion;
			}
			private set
			{
				_cultivosEstacion = value;
				if (_cultivosEstacion != null)
					RaisePropertyChanged("CultivosEstacion");
			}
		}

		#endregion

		#region Private methods

		/// <summary>
		/// 
		/// </summary>
		private void LoadCultivosEstaciones()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					using (PermisosModuleServiceWrapper service = new PermisosModuleServiceWrapper())
					{
						e.Result = service.FindAllCultivosEstaciones();
					}
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						CultivosEstaciones = e.Result as List<CultivosEstacion>;
					else if (e.Error != null)
						ShowMessage(e.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentCultivosEstacion(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if ((bindingSource != null) && (bindingSource.Current != null))
				CultivosEstacion = bindingSource.Current as CultivosEstacion;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnCultivosEstacionesPropertyChanged()
		{
			_expedicionView.cultivosEstacionesBindingSource.DataSource = CultivosEstaciones;
			_expedicionView.cultivosEstacionesBindingSource.CurrentChanged += SetCurrentCultivosEstacion;
			SetCurrentCultivosEstacion(_expedicionView.cultivosEstacionesBindingSource, new EventArgs());
		}

		private void OnCultivosEstacionPropertyChanged()
		{
		}

		#endregion

	}
}